<template>

  <avue-form :key="actFormKey"
             ref="avueForm"
             :option="actFormOption"
             v-model="actFormModel"
             @submit="submitForm"></avue-form>


</template>

<script>
import startActFormApi from "@/api/start/activiti/core/startActFormApi";
import startActFormTools from "@/views/start/activiti/components/startActFormTools";
import avueDataAdapter from "utils/avueDataAdapter";


export default {
  name: "procCloudForm",
  inject: ["getFormConfig", "getFormData", "getFormPropConfig", "getFormIsEdit"],
  props: {},
  data() {
    return {
      actFormKey: Math.random(),
      actFormModel: {},
      actFormOption: {menuBtn: false},
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      let actFormConf = this.getFormConfig();
      startActFormApi.getActFormOption(actFormConf.id).then(this.analysisForm)
    },
    analysisForm(res) {
      let data = res.data.data;
      data.menuBtn = false;
      data.boxType = this.getFormIsEdit() ? "edit" : "view"
      startActFormTools.autoActMainForm(data)
      this.actFormOption = data;
      let actFormModel = this.getFormData() || {}
      let formProps = this.getFormPropConfig() || {}
      for (const col of data.column) {
        let type = formProps[col.prop]
        if (!type) {
          continue
        }
        switch (type) {
          case "view":
            col.editDisplay = true
            col.editDetail = true;
            col.viewDisplay = true;
            col.viewDetail = true;
            break
          case "edit":
            if (data.boxType === "view") {
              col.detail = true;
              break
            }
            col.editDisplay = true
            col.editDisabled = false;
            break
          case "hide":
            col.display = false
            break
        }
      }
      avueDataAdapter.transformDataList(data.column, actFormModel)
      this.actFormModel = actFormModel
      this.actFormKey = Math.random()
    },
    submit() {
      this.$refs.avueForm.submit();
    },
    resetForm() {
      this.$refs.avueForm.resetForm();
    },
    submitForm(data, done) {
      data.businessKey = "";
      let formData = {};
      for (const key in data) {
        if (key.startsWith("$")) {
          continue
        }
        formData[key] = data[key];
      }
      this.$emit("submit", formData, done)
    },

  }
}
</script>

<style scoped>

</style>
